// pages/Index/cashOutGoods/cashOutGoods.js
const controller = require("../controllers/Controller").controller
const app = getApp()



Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:null,
    detail: {},
    shopMsg: {},
    receiveNum: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.code);
    this.setData({
      accountCouponBagId: options.accountCouponBagId,
      code: options.code,
      receiveNum: options.num,
      type: options.type,
    })
   
  },
  onShow:function(){
    this.getGoodsBagDetail(this.data.accountCouponBagId,this.data.code)
  },

  toIndexPage() {
    wx.redirectTo({
      url: '/pages/TradingArea/index',
    })
  },

  phone(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  //用户持有券详情
  getGoodsBagDetail(accountCouponBagId,code) {
    controller.couponBagDetail({
      accountCouponBagId,
      code
    }).then(res => {
      if(res.data.effectiveStartTime){
        res.data.effectiveStartTime = res.data.effectiveStartTime.split(" ")[0].replace(/-/g,'.')
        res.data.effectiveEndTime = res.data.effectiveEndTime.split(" ")[0].replace(/-/g,'.')
      }
      
      console.log(res.data);
      this.setData({
        detail: res.data,
        shopMsg: res.data.shop || {},
      })
    })
  },
  //根据领取码领取赠送的商圈活动 
  receive() {
    controller.giveByCode({
      code: this.data.code
    }).then(res => {
      wx.showToast({
        title: '领取成功',
        icon: 'none'
      })
      setTimeout(() => {
        this.toIndexPage()
      }, 1500)
    })
  },

  // 已领取去往个人中心
  goMyCenter(){
    if (wx.getStorageSync('usertoken')) {
      wx.redirectTo({
        url: '/pages/myCenter/myCenter?type='+this.data.detail.type,
      })
    }else{
      getApp().ToLoginlink()
    }
    
  },
  //跳转商品详情
  toGoodsDetail(e) {
    let shopId = e.currentTarget.dataset.shopid
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/Index/Detail?shopId=' + shopId + "&id=" + id,
    })
  },

  // 活动详情
  toActivePage() {
    let shopGroupId = this.data.detail.shopGroupId
    let id = this.data.detail.couponBagId
    wx.navigateTo({
      url: '/pages/TradingArea/GiftBagDetail?id=' + id + "&shopGroupId=" + shopGroupId ,
    })
  }


})